package com.saunter.controller;

import com.saunter.model.SalaryKey;
import com.saunter.service.SalaryKeyService;
import com.saunter.utils.exception.ExceptionManager;
import com.saunter.utils.response.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Api(tags = {"管理员编辑数据标题"})
@RestController
@RequestMapping("/admin")
public class SalaryKeyController {
    @Autowired
    private SalaryKeyService salaryKeyService;
    @Resource
    private ExceptionManager exceptionManager;

    @ApiOperation("添加数据标题")
    @PostMapping("/addSalaryKey")
    public Map<String, Object> addSalaryKey(@ApiParam(value = "数据记录使用的key", required = true)@RequestParam("id_key")String id_key, @RequestParam("label")String label, @RequestParam(value="remark", required = false)String remark, @ApiParam(value = "权重值",defaultValue = "1") @RequestParam(value = "z_index", defaultValue = "1")int z_index, @ApiParam(value = "0:禁用;1:正常;2:隐藏;", defaultValue = "2")@RequestParam(value = "status", defaultValue = "2")int status) {
        if(1 == salaryKeyService.add(new SalaryKey(id_key, label, remark, new Date(), z_index, status))) {
            return Response.success("添加成功");
        }else {
            throw exceptionManager.create("Sql002");
        }
    }

    @ApiOperation(value = "获取所有数据标题", notes = "包含所有状态")
    @GetMapping("/getSalaryKey/{page_no}/{page_size}")
    public Map<String, Object> getSalaryKey(@PathVariable("page_no")int page_no, @PathVariable("page_size")int page_size,
                                            @ApiParam(value = "0:禁用;1:正常;2:隐藏;")@RequestParam(value = "status", required = false)Integer status, @RequestParam(value = "label", required = false)String label, @RequestParam(value="order_by", required = false)String order_by) {
        Map<String, Object> filters = new HashMap<String, Object>();
        filters.put("status", status);
        filters.put("label", label);
        filters.put("order_by", order_by);
        return Response.success(salaryKeyService.getAll(page_no, page_size, filters));
    }

    @ApiOperation("编辑指定标题")
    @PostMapping("/editSalaryKey")
    public Map<String, Object> editSalaryKey(@ApiParam(value = "数据记录使用的key", required = true)@RequestParam("id_key")String id_key, @ApiParam(value = "0:禁用;1:正常;2:隐藏;")@RequestParam(value = "status", required = false)Integer status, @RequestParam(value = "remark", required = false)String remark, @RequestParam(value = "label", required = false)String label, @ApiParam(value = "权重值")@RequestParam(value = "z_index", required = false)Integer z_index) {
        if(1 == salaryKeyService.edit(new SalaryKey(id_key, label, remark, null, z_index, status))) {
            return Response.success("修改成功");
        }else {
            throw exceptionManager.create("Sql002");
        }
    }

    @ApiOperation("获取启用(除了禁用)的标题")
    @GetMapping("/getSalaryKeyOn")
    public Map<String, Object> getSalaryKeyOn (){
        return Response.success(salaryKeyService.getAllOn());
    }

    @ApiOperation("下载数据记录的导入模板")
    @GetMapping("/getExcelTemplate")
    public void getExcelTemplate(HttpServletResponse response) throws Exception {
        salaryKeyService.getExcelTemplate(response);
    }
}
